/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_X86_LINKAGE_H
#define _ASM_X86_LINKAGE_H

#include <linux/stringify.h>

#undef notrace
#define notrace __attribute__((no_instrument_function))

#ifdef CONFIG_X86_32
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))
#endif /* CONFIG_X86_32 */

#ifdef __ASSEMBLY__

#if defined(CONFIG_X86_64) || defined(CONFIG_X86_ALIGNMENT_16)
#define __ALIGN		.p2align 4, 0x90
#define __ALIGN_STR	__stringify(__ALIGN)
#endif

#if defined(CONFIG_RETHUNK) && !defined(__DISABLE_EXPORTS) && !defined(BUILD_VDSO)
#define RET	jmp __x86_return_thunk
#else /* CONFIG_RETPOLINE */
#ifdef CONFIG_SLS
#define RET	ret; int3
#else
#define RET	ret
#endif
#endif /* CONFIG_RETPOLINE */

#if defined(CONFIG_RTOS_SOFTWARE_CFI)
#define RTOS_CFI_TAG \
	.long CONFIG_RTOS_CFI_TAG;
#define RTOS_CFI_JMP(name) \
	jmp name;
#define RTOS_CFI_ALIGN4 \
	.align 4;
#else
#define RTOS_CFI_TAG
#define RTOS_CFI_JMP(name)
#define RTOS_CFI_ALIGN4
#endif

#define GLOBAL(name)    \
	.globl name;    \
	name:

#define RTOS_GLOBAL_TAG(name) \
	.globl name; \
	RTOS_CFI_TAG \
	name:

#define RTOS_GLOBAL_TAG_JMP(name) \
	RTOS_CFI_JMP(name) \
	.globl name; \
	RTOS_CFI_TAG \
	name:

#define RTOS_GLOBAL_TAG_ALIGN8(name) \
	RTOS_CFI_JMP(name) \
	RTOS_CFI_ALIGN4 \
	.globl name; \
	RTOS_CFI_TAG \
	name:

#if defined(CONFIG_X86_64) || defined(CONFIG_X86_ALIGNMENT_16)
#if defined(CONFIG_RTOS_SOFTWARE_CFI)
#define RTOS_CFI_ALIGN16       .p2alignl 4, CONFIG_RTOS_CFI_TAG
#define RTOS_ENTRY_TAG_ALIGN(align...) \
	RTOS_CFI_ALIGN4 \
	RTOS_CFI_TAG \
	RTOS_CFI_ALIGN16 ;
#endif /* CONFIG_RTOS_SOFTWARE_CFI */
#endif

#else /* !__ASSEMBLY__ */

#if defined(CONFIG_RTOS_SOFTWARE_CFI)
#define RTOS_CFI_STR(R) #R
#define RTOS_CFI_TAG_STR(TAG) \
	".long "RTOS_CFI_STR(TAG)"\n"
#define RTOS_CFI_TAG_STR_TAB(TAG) \
	RTOS_CFI_TAG_STR(TAG)"\t"
#define RTOS_CFI_JMP_STR(SYM) \
	"jmp "RTOS_CFI_STR(SYM)"\n"
#else
#define RTOS_CFI_TAG_STR(TAG)
#define RTOS_CFI_TAG_STR_TAB(TAG)
#define RTOS_CFI_JMP_STR(SYM)
#endif

#if defined(CONFIG_RETHUNK) && !defined(__DISABLE_EXPORTS) && !defined(BUILD_VDSO)
#define ASM_RET	"jmp __x86_return_thunk\n\t"
#else /* CONFIG_RETPOLINE */
#ifdef CONFIG_SLS
#define ASM_RET	"ret; int3\n\t"
#else
#define ASM_RET	"ret\n\t"
#endif
#endif /* CONFIG_RETPOLINE */

#endif /* __ASSEMBLY__ */

#endif /* _ASM_X86_LINKAGE_H */

